package com.lam.mall.common.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.lam.common.utils.CollectionUtil;
import com.lam.common.utils.StringUtils;
import com.lam.mall.entity.SkuProperties;

public class ProductUtil {

	public static List<SkuProperties> skuProperties2List(String properties) {
		
		if(StringUtils.isBlank(properties)) {
			return Collections.emptyList();
		}
		
		String[] ss = properties.split(";");
		List<SkuProperties> list = new ArrayList<>();
		for (String s : ss) {
			String[] pp = s.split(":");
			list.add(new SkuProperties(pp[0], pp[1]));
		}
		return list;
    }
	
	public static String list2SkuProperties(List<SkuProperties> propertiesList) {
    	
    	if(CollectionUtil.isEmpty(propertiesList)) {
    		return "";
    	}
    	StringBuilder sb = new StringBuilder();
		for (SkuProperties pp : propertiesList) {
			sb.append(String.format("%s:%s;", pp.getSpecsName(), pp.getSpecsValue()));
		}
		sb.deleteCharAt(sb.length()-1);
		return sb.toString();
    }
	
	public static List<SkuProperties> prodProperties2List(String properties) {
		if(StringUtils.isBlank(properties)) {
			return Collections.emptyList();
		}
		
		String[] ss = properties.split(";");
		List<SkuProperties> list = new ArrayList<>();
		for (String s : ss) {
			String[] pp = s.split(":");
			list.add(new SkuProperties(pp[0], StringUtils.str2List(pp[1])));
		}
		return list;
    }
	
	public static String list2ProdProperties(List<SkuProperties> propertiesList) {
    	
    	if(CollectionUtil.isEmpty(propertiesList)) {
    		return "";
    	}
    	StringBuilder sb = new StringBuilder();
		for (SkuProperties pp : propertiesList) {
			sb.append(String.format("%s:%s;", pp.getSpecsName(), StringUtils.list2Str(pp.getSpecsValueList())));
		}
		sb.deleteCharAt(sb.length()-1);
		return sb.toString();
    }
}
